#!/bin/bash

PLAYER=$1

test "${ACTION}" = "add" || exit 0
echo "${DEVNAME}" | grep -E "^/dev/input/event[0-9]+$" || exit 0

PARENTHASH=$(evsieve-helper parent "${DEVNAME}" input usb)
BASEFILE="/var/run/virtual-fusion-lightguns-devices.${PARENTHASH}"
PIDFILE="${BASEFILE}.pid"
LOCKFILE="${BASEFILE}.lock"
LOGFILE="${BASEFILE}.log"

unlockAndExit() {
    rmdir "${LOCKFILE}"
    exit "${1}"
}

checkRunningPIDAndExit1() {
    test ! -e "${PIDFILE}" && return 0
    LPID=$(cat "${PIDFILE}")
    test ! -d "/proc/${LPID}" && return 0
    unlockAndExit 1
}

trylock() {
    # lock
    N=0
    while ! mkdir "${LOCKFILE}"
    do
	sleep 1
	let N++
	test "${N}" -gt 30 && exit 1 # give up
    done
}

trylock
checkRunningPIDAndExit1

CHILDREN=$(evsieve-helper children "${PARENTHASH}" input usb | grep -vE "Fusion Piggie [1-4]$") # remove the joystick from the event cause it duplicates events
NDEVS=$(echo "${CHILDREN}" | wc -l)

############
# Old firmware
#
# Player 1
############
# Trigger                  : BTN_LEFT
# L button 1 (Bomb)        : BTN_MIDDLE
# L button 2 (Start)       : KEY_1
# L button 3 (A)           : KEY_ENTER
# R button 1 (Reload)      : BTN_RIGHT
# R button 2 (Select)      : KEY_5
# R button 3 (B)           : KEY_ESC
# Calibration button       : BTN_EXTRA (mouse 5)
# D-pad                    : KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
############
# Player 2 (1.00 firmware)
############
# Trigger                  : BTN_LEFT
# L button 1 (Bomb)        : BTN_MIDDLE
# L button 2 (Start)       : KEY_2
# L button 3 (A)           : KEY_LEFTSHIFT
# R button 1 (Reload)      : BTN_RIGHT
# R button 2 (Select)      : KEY_6
# R button 3 (B)           : KEY_LEFTCTRL
# Calibration button       : BTN_EXTRA (mouse 5)
# D-pad                    : KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT

############
############
# P.I.G.S. 1.7/1.75 firmware
# (based on OpenFIRE)
#
# Player 1
############
# Trigger         : BTN_LEFT
# Pump            : BTN_RIGHT
# Pedal           : BTN_MIDDLE
# A               : KEY_ENTER
# B               : KEY_BACKSPACE
# C (calibration) : BTN_EXTRA
# Start           : KEY_1
# Select          : KEY_5
# D-pad           : KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
############
# Player 2
############
# Trigger  : BTN_LEFT
# Pump     : BTN_RIGHT
# Pedal    : BTN_MIDDLE
# A        : KEY_A
# B        : KEY_B
# C        : BTN_EXTRA
# Start    : KEY_2
# Select   : KEY_6
# D-pad    : KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
############
# Player 3
############
# Trigger  : BTN_LEFT
# Pump     : BTN_RIGHT
# Pedal    : BTN_MIDDLE
# A        : KEY_C
# B        : KEY_D
# C        : BTN_EXTRA
# Start    : KEY_3
# Select   : KEY_7
# D-pad    : KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
############
# Player 4
############
# Trigger  : BTN_LEFT
# Pump     : BTN_RIGHT
# Pedal    : BTN_MIDDLE
# A        : KEY_E
# B        : KEY_F
# C        : BTN_EXTRA
# Start    : KEY_4
# Select   : KEY_8
# D-pad    : KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT

if test "${NDEVS}" = 2
then
    DEV1=$(echo "${CHILDREN}" | head -1           | cut -f 1)
    DEV2=$(echo "${CHILDREN}" | head -2 | tail -1 | cut -f 1)

    if test "${PLAYER}" = "1"
    then
	nohup evsieve                                \
            --input "${DEV1}" "${DEV2}" persist=exit \
            --map yield btn:middle     btn:right     \
            --map yield key:1          btn:middle    \
            --map yield key:5          btn:1         \
            --map yield key:enter      btn:2         \
            --map yield key:esc        btn:3         \
            --map yield key:up         btn:5         \
            --map yield key:down       btn:6         \
            --map yield key:left       btn:7         \
            --map yield key:right      btn:8         \
            --output name="Fusion Lightgun" >/dev/null 2>"${LOGFILE}" &
    fi

    if test "${PLAYER}" = "2"
    then
	nohup evsieve                                \
            --input "${DEV1}" "${DEV2}" persist=exit \
            --map yield btn:middle     btn:right     \
            --map yield key:2          btn:middle    \
            --map yield key:6          btn:1         \
            --map yield key:leftshift  btn:2         \
            --map yield key:leftctrl   btn:3         \
            --map yield key:up         btn:5         \
            --map yield key:down       btn:6         \
            --map yield key:left       btn:7         \
            --map yield key:right      btn:8         \
            --output name="Fusion Lightgun" >/dev/null 2>"${LOGFILE}" &
    fi

    if test "${PLAYER}" = "3"
    then
	nohup evsieve                                \
            --input "${DEV1}" "${DEV2}" persist=exit \
            --map yield btn:middle     btn:right     \
            --map yield key:1          btn:middle    \
            --map yield key:5          btn:1         \
            --map yield key:enter      btn:2         \
            --map yield key:backspace  btn:3         \
            --map yield key:up         btn:5         \
            --map yield key:down       btn:6         \
            --map yield key:left       btn:7         \
            --map yield key:right      btn:8         \
            --output name="Fusion Lightgun" >/dev/null 2>"${LOGFILE}" &
    fi

    if test "${PLAYER}" = "4"
    then
	nohup evsieve                                \
            --input "${DEV1}" "${DEV2}" persist=exit \
            --map yield btn:middle     btn:right     \
            --map yield key:2          btn:middle    \
            --map yield key:6          btn:1         \
            --map yield key:a          btn:2         \
            --map yield key:b          btn:3         \
            --map yield key:up         btn:5         \
            --map yield key:down       btn:6         \
            --map yield key:left       btn:7         \
            --map yield key:right      btn:8         \
            --output name="Fusion Lightgun" >/dev/null 2>"${LOGFILE}" &
    fi

    if test "${PLAYER}" = "5"
    then
	nohup evsieve                                \
            --input "${DEV1}" "${DEV2}" persist=exit \
            --map yield btn:middle     btn:right     \
            --map yield key:3          btn:middle    \
            --map yield key:7          btn:1         \
            --map yield key:c          btn:2         \
            --map yield key:d          btn:3         \
            --map yield key:up         btn:5         \
            --map yield key:down       btn:6         \
            --map yield key:left       btn:7         \
            --map yield key:right      btn:8         \
            --output name="Fusion Lightgun" >/dev/null 2>"${LOGFILE}" &
    fi

    if test "${PLAYER}" = "6"
    then
	nohup evsieve                                \
            --input "${DEV1}" "${DEV2}" persist=exit \
            --map yield btn:middle     btn:right     \
            --map yield key:4          btn:middle    \
            --map yield key:8          btn:1         \
            --map yield key:e          btn:2         \
            --map yield key:f          btn:3         \
            --map yield key:up         btn:5         \
            --map yield key:down       btn:6         \
            --map yield key:left       btn:7         \
            --map yield key:right      btn:8         \
            --output name="Fusion Lightgun" >/dev/null 2>"${LOGFILE}" &
    fi

    echo $! > "${PIDFILE}"
else
    unlockAndExit 1
fi

unlockAndExit 0
